package com.jack.leetcode.eachproblem_2020;

public class 单调递增的数字 {

    public int monotoneIncreasingDigits(int N) {
        String s = N + "";
        char[] c = s.toCharArray();
        int max = -1, idx = -1;
        for(int i = 0;i < s.length() - 1;i++){
            if(max < c[i]){
                max = c[i];
                idx = i;
            }
            if(c[i] > c[i + 1]){
                c[idx] -= 1;
                for(int j = idx + 1;j < s.length();j++){
                    c[j] = '9';
                }
                break;
            }
        }
        return Integer.parseInt(new String(c));
    }
}
